<?php

namespace App\Transformers\Traits;

use App\Transformers\Exceptions\NotFoundResponseStatusException;

/**
 * Trait HttpResponse
 * @package App\Transformers\Traits
 * @method ok($data = null)
 * @method created($data = null)
 * @method accepted($data = null)
 * @method nonAuthoritativeInformation($data = null)
 * @method noContent($data = null)
 * @method resetContent($data = null)
 * @method partialContent($data = null)
 * @method multiStatus($data = null)
 * @method alreadyReported($data = null)
 * @method badRequest($data = null)
 * @method unauthorized($data = null)
 * @method paymentRequired($data = null)
 * @method forbidden($data = null)
 * @method notFound($data = null)
 * @method methodNotAllowed($data = null)
 * @method notAcceptable($data = null)
 * @method proxyAuthenticationRequired($data = null)
 * @method requestTimeout($data = null)
 * @method conflict($data = null)
 * @method gone($data = null)
 * @method lengthRequired($data = null)
 * @method preconditionFailed($data = null)
 * @method payloadTooLarge($data = null)
 * @method uRITooLong($data = null)
 * @method unsupportedMediaType($data = null)
 * @method rangeNotSatisfiable($data = null)
 * @method expectationFailed($data = null)
 * @method misdirectedRequest($data = null)
 * @method unprocessableEntity($data = null)
 * @method locked($data = null)
 * @method failedDependency($data = null)
 * @method upgradeRequired($data = null)
 * @method preconditionRequired($data = null)
 * @method tooManyRequests($data = null)
 * @method requestHeaderFieldsTooLarge($data = null)
 * @method unavailableForLegalReasons($data = null)
 */
trait HttpResponse
{
    protected $status = [
        200 => 'ok',
        201 => 'created',
        202 => 'accepted',
        203 => 'nonAuthoritativeInformation',
        204 => 'noContent',
        205 => 'resetContent',
        206 => 'partialContent',
        207 => 'multiStatus',
        208 => 'alreadyReported',
        400 => 'badRequest',
        401 => 'unauthorized',
        402 => 'paymentRequired',
        403 => 'forbidden',
        404 => 'notFound',
        405 => 'methodNotAllowed',
        406 => 'notAcceptable',
        407 => 'proxyAuthenticationRequired',
        408 => 'requestTimeout',
        409 => 'conflict',
        410 => 'gone',
        411 => 'lengthRequired',
        412 => 'preconditionFailed',
        413 => 'payloadTooLarge',
        414 => 'uRITooLong',
        415 => 'unsupportedMediaType',
        416 => 'rangeNotSatisfiable',
        417 => 'expectationFailed',
        421 => 'misdirectedRequest',
        422 => 'unprocessableEntity',
        423 => 'locked',
        424 => 'failedDependency',
        426 => 'upgradeRequired',
        428 => 'preconditionRequired',
        429 => 'tooManyRequests',
        431 => 'requestHeaderFieldsTooLarge',
        451 => 'unavailableForLegalReasons',
    ];

    public function __call($name, $data)
    {
        $status = array_search($name, $this->status);

        if (is_bool($status))
            throw new NotFoundResponseStatusException();

        return response()->json($data[0] ?? null, $status);
    }
}